package hms.database;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

/**
 * Generic class to cover the generation, usage, and maintenance of a JList that represents
 * data from the database that has been stored in a class that extends DBRowList.
 * 
 * Should instantiate with a filled-in copy of the DBRowList.  When changes are made,
 * call update() to update the JList view.
 * 
 * The toString() method should be overridden in the DBRowObject to display what you want
 * to display in the JList.
 * 
 * See the commented-out main function for some example code.
 * 
 * @author Ben
 *
 * @param <DBList> An object that extends DBRowList
 * @param <E> 
 */
public class DBJFrame<DBList extends DBRowList<E>, E extends DBRowObject> extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6538562590110631772L;

	private DBList table;
	private DefaultListModel listModel;
	private JList list;
	private JScrollPane scrollPane;

	public DBJFrame(DBList DBTable) {
		super();
		table = DBTable;
		listModel = new DefaultListModel();
		list = new JList(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane = new JScrollPane(list);
		update();
		getContentPane().add(scrollPane);
	}

	public void update() {
		listModel.clear();
		listModel.ensureCapacity(table.size());
		for (E ua : table) {
			if (!ua.isDisplayable()) {
				listModel.addElement(ua);
			}
		}
		list.validate();
	}

	/**
	 * Get the DBRowObject at the selected index, or null if nothing is selected.
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public E getSelectedRowObject() {
		if (list.getSelectedIndex() == -1) {
			return null;
		} else {
			return (E) listModel.get(list.getSelectedIndex());
		}
	}

	public DBList getSource() {
		return table;
	}
	
	public JList getJList() {
		return list;
	}

	public E showDialog(String title) {
		Object[] options = { "OK", "Cancel" };

		int response = JOptionPane.showOptionDialog(this, this.getContentPane(), title,
				JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
				options, options[1]);

		if(response == JOptionPane.OK_OPTION) {
			return this.getSelectedRowObject();
		} else {
			return null;
		}
	}
	
	public void addToList(E obj) {
		int index = listModel.size();
		table.add(obj);
		update();
		list.setSelectedIndex(index);
	}
	
	@SuppressWarnings("unchecked")
	public void removeSelected() {
		int index = list.getSelectedIndex();
		if(list.getSelectedIndex()>=0) {
			((E)listModel.get(index)).markToBeDeleted();
			update();
			if(index==listModel.getSize()) {
				list.setSelectedIndex(index-1);
			} else if(listModel.getSize()!=0) {
				list.setSelectedIndex(index);
			}
		}
	}
	
	public void commitToDatabase() {
		table.commitToDatabase();
	}
/*
	public static void main(String args[]) {

		DBJFrame<UserAccountControl,UserAccount> frame = new DBJFrame<UserAccountControl, UserAccount>(new UserAccountControl());
		frame.getSource().add(new UserAccount("bob","bob"));
		frame.update();
		UserAccount ua = frame.showDialog("Choose a User Account");

		if (ua == null) {
			System.out.println("Nothing returned");
		} else {
			System.out.println(ua.toString());
		}
		System.exit(0);
	}
	*/

}
